package com.foodchain.controller;

import com.foodchain.bean.ResultBean;
import com.foodchain.entity.ActivityUser;
import com.foodchain.service.ActivityUserService;
import com.foodchain.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
/**
 * @ClassName 活动用户
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 13:46
 * @Version 1.0
 **/
@Controller
@RequestMapping("/activityUser")
public class ActivityUserController extends BaseController{

    @Resource
    private ActivityUserService activityUserService;
    /*
     * 功能描述:用户报名活动
     * @param:  * @param activityUser
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:42
     */
    @ResponseBody
    @RequestMapping("/apply")
    public String apply(ActivityUser activityUser){
         ActivityUser user = activityUserService.findByUserIdAndActivityId(activityUser);
        ResultBean bean = new ResultBean();
        if(user!=null){
            bean.setCode(1);
            bean.setMessage("您已报名该活动");
            return JsonCode(0,"success",bean);
        }
        activityUserService.saveApplyUser(activityUser);
        bean.setCode(0);
        bean.setMessage("报名成功，正在审核...");
        return JsonCode(0,"success",bean);

    }
     /*
      * 功能描述:通过用户id查询活动信息
      * @param:  * @param page
      * @param size
      * @param userId
      * @return: java.lang.String
      * @auther: duyuanyuan
      * @date: 2019/3/25 14:29
      */
    @ResponseBody
    @RequestMapping("/findByUserId")
    public String findByUserId( @RequestParam(value = "page", defaultValue = "0") Integer page,
                                @RequestParam(value = "size", defaultValue = "10") Integer size,String  userId){
        if(Misc.isEmpty(userId)){ return JsonCode(1,"error",null);}
        Page<ActivityUser>  activityUsers = activityUserService.findByUserId(userId,page,size);
        return JsonCode(0,"success",activityUsers);
    }
}
